#include "../Elogs/bitlog.h"
#include <unistd.h>

void test_log(const std::string &name)
{
    INFO("%s", "测试开始");
    log::Logger::ptr logger = log::LoggerManager::getInstance().getLogger(name);
    logger->debug("%s", "测试日志");
    logger->info("%s", "测试日志");
    logger->warn("%s", "测试日志");
    logger->error("%s", "测试日志");
    logger->fatal("%s", "测试日志");
    INFO("%s", "测试完毕");
}

int main()
{
    std::unique_ptr<log::LoggerBuilder> builder(new log::GlobalLoggerBuilder());
    builder->buildLoggerName("async_logger");
    builder->buildLoggerLevel(log::LogLevel::value::DEBUG);
    builder->buildLoggerFormatter("[%c][%f:%l][%p]%m%n");
    builder->buildLoggerType(log::LoggerType::LOGGER_SYNC);
    builder->buildSink<log::FileSink>("./logfile/async.log");
    builder->buildSink<log::StdoutSink>();
    builder->buildSink<log::RollBySizeSink>("./logfile/roll-sync-by-size", 1024 * 1024);
    builder->build();
    test_log("async_logger");
    return 0;
}

// int main()
// {
//     std::unique_ptr<log::LoggerBuilder> builder(new log::GlobalLoggerBuilder());
//     builder->buildLoggerName("async_logger");
//     builder->buildLoggerLevel(log::LogLevel::value::WARN);
//     builder->buildLoggerFormatter("[%c][%f:%l]%m%n");
//     builder->buildLoggerType(log::LoggerType::LOGGER_ASYNC);
//     builder->buildEnableUnSafeAsync();
//     builder->buildSink<log::FileSink>("./logfile/async.log");
//     builder->buildSink<log::StdoutSink>();
//     builder->build();
//     test_log();
// 读取文件数据，慢慢写入缓冲区,最终将生成的新文件与源文件的大小进行比较
// std::ifstream ifs("./logfile/test.log", std::ios::binary);
// if (ifs.is_open() == false)
// {
//     std::cout << "open fail!\n";
//     return -1;
// }
// ifs.seekg(0, std::ios::end); // 读写位置跳转至文件结尾
// size_t fsize = ifs.tellg();  // 获取当前读写位置相对于起始位置的偏移量
// ifs.seekg(0, std::ios::beg); // 重新跳转至起始位置
// std::string body;
// body.resize(fsize);
// ifs.read(&body[0], fsize);
// if (ifs.good() == false)
// {
//     std::cout << "read error!\n";
//     return -1;
// }
// ifs.close();

// std::cout << fsize << std::endl;
// log::Buffer buffer;
// for (int i = 0; i < body.size(); i++)
// {
//     buffer.push(&body[i], 1);
// }
// // 此时buffer已经存储上述文件的数据
// std::cout << buffer.readAbleSize() << std::endl;
// // 将buffer中存储的数据写入到tmp.log新文件中
// std::ofstream ofs("./logfile/tmp.log", std::ios::binary);
// ofs.write(buffer.begin(), buffer.readAbleSize()); // 将buffer可读数据总长度全部写入ofs指向的tmp.log新文件中
// size_t oldsize = buffer.readAbleSize();
// for (size_t i = 0; i < oldsize; i++)
// {
//     ofs.write(buffer.begin(), 1);
//      if(ofs.good()==false) {std::cout<<"write error\n";return -1;}
//     buffer.moveReader(1); // 将可读指针向后移
// }
// if (ofs.good() == false)
// {
//     std::cout << "write error\n";
//     return -1;
// }
// ofs.close();
//     std::string logger_name = "sync_logger";
//     log::LogLevel::value limit = log::LogLevel::value::WARN;
//     log::Formatter::ptr fmt(new log::Formatter("[%d{%H:%M:%S}][%c][%f:%l][%p]%T%m%n"));
//     log::LogSink::ptr stdout_lsp = log::SinkFactor::create<log::StdoutSink>();
//     log::LogSink::ptr file_lsp = log::SinkFactor::create<log::FileSink>("./logfile/test.log");
//     log::LogSink::ptr roll_lsp = log::SinkFactor::create<log::RollBySizeSink>("./logfile/roll-", 1024 * 1024);
//     std::vector<log::LogSink::ptr> sinks = {stdout_lsp, file_lsp, roll_lsp};
//     log::Logger::ptr logger(new log::SyncLogger(logger_name, limit, fmt, sinks));
// std::unique_ptr<log::LoggerBuilder> builder(new log::LocalLoggerBuilder());
// builder->buildLoggerName("sync_logger");
// builder->buildLoggerLevel(log::LogLevel::value::WARN);
// builder->buildLoggerFormatter("%m%n");
// builder->buildLoggerType(log::LoggerType::LOGGER_SYNC);
// builder->buildSink<log::FileSink>("./logfile/test.log");
// builder->buildSink<log::StdoutSink>();
// log::Logger::ptr logger = builder->build();

// logger->debug(__FILE__, __LINE__, "%s", "测试日志");
// logger->info(__FILE__, __LINE__, "%s", "测试日志");
// logger->warn(__FILE__, __LINE__, "%s", "测试日志");
// logger->error(__FILE__, __LINE__, "%s", "测试日志");
// logger->fatal(__FILE__, __LINE__, "%s", "测试日志");

// size_t cur_size = 0, count = 0;
// while (cur_size < 1024 * 1024 * 10)
// {
//     logger->fatal(__FILE__, __LINE__, "测试日志-%d", count++);
//     cur_size += 20;
// }
// log::LogMessage msg(log::LogLevel::value::INFO, 8, "test.cc", "root", "格式化功能测试...");
// log::Formatter fmt;
// std::string str = fmt.format(msg);
// // log::LogSink::ptr stdout_lsp = log::SinkFactor::create<log::StdoutSink>();
// // log::LogSink::ptr file_lsp = log::SinkFactor::create<log::FileSink>("./logfile/test.log");
// // log::LogSink::ptr roll_lsp = log::SinkFactor::create<log::RollBySizeSink>("./logfile/roll-", 1024 * 1024);
// log::LogSink::ptr time_lsp = log::SinkFactor::create<RollByTimeSink>("./logfile/roll-", TimeGap::GAP_SECOND);
// time_t old = log::util::Date::getTime();
// while (log::util::Date::getTime() < old + 5)
// {
//     time_lsp->log(str.c_str(), str.size());
//     sleep(1);
// }
// stdout_lsp->log(str.c_str(), str.size());
// stdout_lsp->log(str.c_str(), str.size());
// file_lsp->log(str.c_str(), str.size());
// size_t cur_size = 0;
// size_t count = 0;
// while (cur_size < 1024 * 1024 * 10)
// {
//     std::string tmp = str + std::to_string(count++);
//     roll_lsp->log(tmp.c_str(), tmp.size());
//     cur_size += tmp.size();
// }
// std::cout << log::LogLevel::toString(log::LogLevel::value::DEBUG) << std::endl;
// std::cout << log::LogLevel::toString(log::LogLevel::value::INFO) << std::endl;
// std::cout << log::LogLevel::toString(log::LogLevel::value::WARN) << std::endl;
// std::cout << log::LogLevel::toString(log::LogLevel::value::ERROR) << std::endl;
// std::cout << log::LogLevel::toString(log::LogLevel::value::FATAL) << std::endl;
// std::cout << log::LogLevel::toString(log::LogLevel::value::OFF) << std::endl;
// std::cout << log::util::Date::getTime() << std::endl;
// std::string pathname = "./abc/bcd/a.txt";
// log::util::File::createDirectory(log::util::File::path(pathname));
// char data[] = "Hello Binary World!";
// std::cout.write(data, 5); // 只写入前5个字节 "Hello"
// return 0;
//     return 0;
// }
